#include "historical.h"
#include "ui_historical.h"

Historical::Historical(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Historical)
{
    ui->setupUi(this);
    QSqlQuery query(db);

      QString SQLselect = QString("select sender,receiver,message,time from allmessage where isGroup = 1;");
      if(query.exec(SQLselect))
      {
          while(query.next())
           {
              QString qsender = query.value(0).toString();
              QString qreceiver = query.value(1).toString();
               QString qmessage = query.value(2).toString();
               QString qtime = query.value(3).toString();
               QString addtogether = QString("<b><font size='4' color='blue'>sender: %1      </font></b><b><font size='4' color='green'>receiver: %2  </font></b><br><b><font size='2' color='grey'>time: %3  </font></b><br><b><font size='4' color='black'>%4</font></b><br><br>").arg(qsender,qreceiver,qtime,qmessage);
              ui->textBrowser->append(addtogether);
           }
      }

}

Historical::~Historical()
{
    delete ui;
}



void Historical::on_deletepushButton_clicked()//删除历史消息记录
{
    QSqlQuery query(db);
    QString SQLdelete = QString("drop table allmessage;");//删除信息表
    if(!query.exec(SQLdelete)){
        qDebug()<<"删除历史消息失败";
        QMessageBox::warning(nullptr,tr("警告"),tr("删除历史消息失败"),QMessageBox::Ok);
    }
    else
        qDebug()<<"删除历史消息成功";
        QMessageBox::information(nullptr,tr("信息"),tr("删除历史消息成功"),QMessageBox::Ok);
    //创建信息表
    QString create_2=QString("CREATE TABLE allmessage ( sender TEXT, receiver TEXT, message TEXT, time TEXT, isGroup INTEGER )");
    query.exec(create_2);
    ui->textBrowser->setText("");
}
